package database;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;


public class MySql {

	private Connection getConn() 
	{
        Connection conn     = null;
        String url          = "jdbc:mysql://127.0.0.1:3306/";
        String db           = "sudeep";
        String driver       = "com.mysql.jdbc.Driver";
        String user         = "root";
        String pass         = "root";            
        try 
        {
            Class.forName(driver).newInstance();
        } 
        catch (InstantiationException e) 
        {
            e.printStackTrace();
        } 
        catch (IllegalAccessException e) 
        {
            e.printStackTrace();
        } 
        catch (ClassNotFoundException e) 
        {
            e.printStackTrace();
        }
        
        try 
        {
          conn = DriverManager.getConnection(url+db, user, pass);
        } 
        catch (SQLException e) 
        {
            System.err.println("Mysql Connection Error: ");
            e.printStackTrace();
        }
        return conn;
     }	
	
	private String getDBData()
	{
		String query = "SELECT * FROM login";
		String retVal = "fail"; 
		try 
		{
		    Connection conn = this.getConn();
		    if(conn == null)
		    {
		    	retVal = "fail connection";
		    }
		    else
		    {
		      Statement select = conn.createStatement();
		      ResultSet result = select.executeQuery(query);
		      while (result.next()) 
		      {
		    	retVal = result.getString(1);
		      }
		      select.close();
		      result.close();
		      conn.close();
		    }
		 } 
		catch(SQLException e) 
		{
		  System.err.println("Mysql Statement Error: " + query);
		  e.printStackTrace();
		}
		return retVal;
	}
	
	
	
	public String getMyName()
	{
		return getDBData();
	}
	
}
